//
//  BaseNavigationController.swift
//  sinaWeibo
//
//  Created by liii on 16/8/21.
//  Copyright © 2016年 liii. All rights reserved.
//

import UIKit

class BaseNavigationController: UINavigationController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 如果自定义导航条的返回按钮, 对应的返回手势的代理对象, 需要重新设定
        self.interactivePopGestureRecognizer?.delegate = self
    }
    
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        //如果是根控制器, 就让手势失效, 否则有效
        return childViewControllers.count != 0
    }
    
    // 重写此方法, 统一处理tabBar隐藏的问题(注意在实例化一个导航控制器的时候, 默认会调用这个方法)
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        // 如果不是最顶级的(外层)控制器
        if childViewControllers.count != 0 {
            
            // 如果自定义导航条的返回按钮, 对应的返回手势的代理对象, 需要重新设定
            let btn = UIFactory.buttonWithTitle("返回", fontSize: nil, color: UIColor.darkGrayColor(), backgroundImage: nil, image: "navigationbar_back_withtext")
            btn.addTarget(self, action: "back", forControlEvents: .TouchUpInside)
            
            let customItem = UIBarButtonItem(customView: btn)
            viewController.navigationItem.leftBarButtonItem = customItem
            
            // 隐藏tabBar
            viewController.hidesBottomBarWhenPushed = true
        }
        super.pushViewController(viewController, animated: animated)
    }
    
    @objc private func back() {
        self.popViewControllerAnimated(true)
    }
    
}
